<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>{{ isEdit ? '编辑课程' : '新增课程' }}</span>
        </div>
      </template>

      <el-form
        ref="courseFormRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        style="max-width: 600px"
      >
        <el-form-item label="课程名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入课程名称" />
        </el-form-item>
        
        <el-form-item label="课程描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入课程描述"
          />
        </el-form-item>
        
        <el-form-item label="分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择分类" clearable>
            <el-option
              v-for="item in categoryOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="价格" prop="price">
          <el-input-number
            v-model="form.price"
            :min="0"
            controls-position="right"
          />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getCourseById, saveOrUpdateCourse } from '@/api/course'

// 路由相关
const route = useRoute()
const router = useRouter()

// 表单引用
const courseFormRef = ref()

// 是否为编辑模式
const isEdit = ref(false)

// 分类选项
const categoryOptions = ref([])

// 表单数据
const form = reactive({
  id: undefined,
  name: '',
  description: '',
  categoryId: '',
  price: 0,
  status: 1
})

// 表单验证规则
const rules = {
  name: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
  categoryId: [{ required: true, message: '请选择分类', trigger: 'blur' }]
}

// 获取分类选项数据
const getCategoryOptions = async () => {
  // 这里需要调用相应的API获取选项数据
  // 暂时使用模拟数据
  categoryOptions.value = [
    { id: 1, name: '编程语言' },
    { id: 2, name: '数据分析' }
  ]
}

// 获取课程详情
const getCourseDetail = async (id) => {
  try {
    const response = await getCourseById(id)
    Object.assign(form, response.data)
  } catch (error) {
    ElMessage.error('获取课程信息失败')
  }
}

// 提交表单
const submitForm = async () => {
  if (!courseFormRef.value) return
  
  await courseFormRef.value.validate((valid) => {
    if (valid) {
      saveOrUpdateCourse(form)
        .then(() => {
          ElMessage.success('保存成功')
          goBack()
        })
        .catch(() => {
          ElMessage.error('保存失败')
        })
    }
  })
}

// 返回上一页
const goBack = () => {
  router.push('/courses')
}

// 初始化
onMounted(() => {
  getCategoryOptions()
  
  // 判断是否为编辑模式
  if (route.params.id) {
    isEdit.value = true
    getCourseDetail(route.params.id)
  }
})
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>